package com.puhui.chome.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.puhui.chome.model.vo.Attachment;

/**
 * 由于sendCloud的Messaeg没有实现Serializable接口 必须封装一个实体类用于接口调用
 * 
 * @author zhangming
 */
public class EmailMessageInfo implements Serializable {

    private static final long serialVersionUID = -4153501235378742053L;
    /** 发件人名称 */
    private String fromName;
    /** 发件人地址 */
    private String fromAddress;
    /** 收件人列表 */
    private List<String> recipients;
    /** 主题 */
    private String subject;
    /** 抄送者列表 */
    private List<String> cc;
    /** 增加密送者列表 */
    private List<String> bcc;
    /** 邮件消息内容 */
    private String body;
    /** 邮件附件 */
    private List<Attachment> attachments;
    /** 是否是HTML邮件 */
    private boolean html;

    public EmailMessageInfo() {
        recipients = new ArrayList<String>(10);
        cc = new ArrayList<String>(5);
        bcc = new ArrayList<String>(5);
        attachments = new ArrayList<Attachment>(10);
    }

    public String getFromName() {
        return fromName;
    }

    public void setFromName(String fromName) {
        this.fromName = fromName;
    }

    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public List<String> getRecipients() {
        return recipients;
    }

    public List<String> getCc() {
        return cc;
    }

    public List<String> getBcc() {
        return bcc;
    }

    public List<Attachment> getAttachments() {
        return attachments;
    }

    // 添加收件人
    public void addRecipient(String email) {
        this.recipients.add(email);
    }

    // 添加多个收件人
    public void addRecipients(List<String> emails) {
        this.recipients.addAll(emails);
    }

    // 添加抄送人
    public void addCc(String cc) {
        this.cc.add(cc);
    }

    // 添加多个抄送人
    public void addCcs(List<String> ccs) {
        this.cc.addAll(ccs);
    }

    // 添加密送
    public void addBcc(String bcc) {
        this.bcc.add(bcc);
    }

    // 添加多个密送
    public void addBccs(List<String> bccs) {
        this.bcc.addAll(bccs);
    }

    // 添加附件
    public void addAttachment(String alias, String filePath) {
        if (null == filePath || "".equals(filePath.trim())) {
            throw new RuntimeException("文件路径名为空");
        }
        if (null == alias || "".equals(alias.trim())) {
            throw new RuntimeException("文件别名为空");
        }

        addAttachment(alias, new File(filePath));
    }

    // 添加附件
    public void addAttachment(String alias, File file) {
        if (null == alias || "".equals(alias.trim())) {
            throw new RuntimeException("文件别名为空");
        }

        if (null == file) {
            throw new RuntimeException("附件为空");
        }

        if (file.isDirectory()) {
            throw new RuntimeException("附件不能是一个目录");
        }

        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[102400];
            while ((len = fis.read(buffer, 0, buffer.length)) != -1) {
                bos.write(buffer, 0, len);
            }
            byte[] data = bos.toByteArray();
            Attachment att = new Attachment();
            att.setData(data);
            att.setAlias(alias);
            addAttachment(att);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
                if (null != fis) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 添加附件
    public void addAttachment(Attachment attachment) {
        if (null == attachment.getAlias() || "".equals(attachment.getAlias())) {
            throw new RuntimeException("文件别名不能为空");
        }
        if (null == attachment.getData() || attachment.getData().length <= 0) {
            throw new RuntimeException("附件的内容是空的");
        }
        this.attachments.add(attachment);
    }

    // 添加多个附件
    public void addAttachment(Attachment[] attachments) {
        this.attachments.addAll(Arrays.asList(attachments));
    }

    public boolean isHtml() {
        return html;
    }

    public void setHtml(boolean html) {
        this.html = html;
    }
}
